package com.power.structure.stack;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/6/11 10:35 下午
 */
public class LinkedStack {
    Node head;
    Node tail;
    private int size;

    class Node {
        Node next;
        int value;

        public Node(int value) {
            this.value = value;
        }
    }

    public int size() {
        return size;
    }

    public void push(int value) {
        Node node = new Node(value);
        if (size == 0) {
            tail = head = node;
        } else {
            tail.next = node;
            tail = node;
        }
        size++;
    }

    public boolean isEmpty() {
        if (size == 0) {
            return true;
        }
        return false;
    }

    public int pop() {
        if (isEmpty()) {
            throw new IllegalStateException("没有数据了");
        }
        int res = head.value;
        head = head.next;
        size--;
        return res;
    }

    public void display() {
        Node tmp = head;
        while (tmp != null) {
            System.out.print(" " + tmp.value);
            tmp = tmp.next;
        }
        System.out.println("\n");
    }
    
    public static void main(String[] args) {
        LinkedStack stack = new LinkedStack();
        for (int i = 0; i < 21; i++) {
            stack.push(i);
        }
        stack.display();

        System.out.println("\n");
        while (stack.size > 0) {
            System.out.print(" " + stack.pop());
        }
        System.out.println();
    }
}
